<?php 
include('common_include.php');
include('validateSession.php');

$announcementArr = array();
$dao = new SQLitePersistence();
$announcementArr = $dao->getAllAnnouncement();
unset($dao);
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script language="javascript" src="scripts/common.js"></script>
<title>
	Update Announcement
</title>
<script>

function submitForAdd() {
	var opsElem = document.forms[0].operation;
	if (opsElem) {
		opsElem.value = 'Add';
	}
	var addAreaText = trim(document.forms[0].addAnnouncementArea.value);
	if (addAreaText == '') {
		alert('Please enter some announcement for new announcement.');
		document.forms[0].addAnnouncementArea.focus();
		return;
	}
	document.forms[0].submit();
}

function submitForUpdate() {
	var announcementResultSize = <?php echo sizeof($announcementArr);?>;
	if (!validateSequences(announcementResultSize)) {
		return;
	}
	for (i=0; i < announcementResultSize; i++) {
		var descElem = document.getElementById('announcement[' + i + '][announcementDesc]');
		if (descElem) {
			if (trim(descElem.value) == '') {
				alert('Description of the announcement cannot be empty.');
				return;
			}
		}
	}
	var opsElem = document.forms[0].operation;
	if (opsElem) {
		opsElem.value = 'Update';
	}
	document.forms[0].submit();
}

function submitForDelete(announcementID) {
	var opsElem = document.forms[0].operation;
	if (opsElem) {
		opsElem.value = 'Delete';
	}	
	document.forms[0].deleteID.value = announcementID;
	document.forms[0].submit();
}

function initAllSelectComponents() {
	var announcementResultSize = <?php echo sizeof($announcementArr);?>; 

	for (i=0; i < announcementResultSize; i++) {
		var selectComponent = document.getElementById('sequence_' + i);
		var initSeqComponent = document.getElementById('initSeq_' + i);
		if (selectComponent) {
			for (j=0; j < announcementResultSize; j++) {
				var optionElem = document.createElement('option');
				optionElem.text = String(j+1);
				optionElem.value = String(j+1);
				if (optionElem.value == initSeqComponent.value) {
					optionElem.selected = true;
				}
			    try {
			    	selectComponent.add(optionElem, null); // standards compliant; doesn't work in IE
				}
				catch(ex) {
					selectComponent.add(optionElem); // IE only
				}
			}
		}
	}
	
}

</script>
</head>
<body onload="initAllSelectComponents();">

<form action="UpdateAnnouncementController.php" method="post">
<table>
	<tr>
		<td class="errorLabel">
			<?php // echo $_GET['error']; ?>
		</td>
	</tr>
	<tr>
		<td class="labelClass">
			Announcement Listing:
		</td>
	</tr>
	<tr>
		<td class="labelClass">
			<table width="100%" id="dynamicTable">
				<tr>
					<td class="textLabelClass">
						Announcement Description:
					</td>
					<td class="textLabelClass">
						Announcement Order Sequence:
					</td>
					<td class="textLabelClass">
						&nbsp;
					</td>
				</tr>
				<?php for ($i=0; $i < sizeof($announcementArr); $i++) {
					$rowIDAttr = 'sequence_' . $i;
					$hiddenInitSeq = 'initSeq_' . $i;
					$anAnnouncement = $announcementArr[$i];
					$submitForDeleteOnClickEvent = 'submitForDelete(' . $anAnnouncement->getAnnouncementId() . ');';
					$announcementIDHidden = 'announcement[' . $i . '][announcementID]';
					$announcementDescArea = 'announcement[' . $i . '][announcementDesc]';
					$announcementSeqSelect = 'announcement[' . $i . '][announcementSeq]';
				?>
				<tr>
					<td>
						<input type="hidden" name="<?php echo $announcementIDHidden?>" value="<?php echo $anAnnouncement->getAnnouncementId();?>"/>
						<textarea rows="8" cols="50" id="<?php echo $announcementDescArea?>" name="<?php echo $announcementDescArea?>"><?php echo $anAnnouncement->getAnnouncementDesc();?></textarea>
					</td>
					<td>
						<select name="<?php echo $announcementSeqSelect?>" id="<?php echo $rowIDAttr?>">
						</select>
					</td>
					<td>
						<input type="button" value="Delete" onclick="<?php echo $submitForDeleteOnClickEvent?>"/>
						<input type="hidden" id="<?php echo $hiddenInitSeq?>" name="<?php echo $hiddenInitSeq?>" value="<?php echo $anAnnouncement->getAnnouncementSeq();?>"/>
					</td>					
				</tr>
				<?php } 
				unset($announcementArr);?>
			</table>
		</td>
	</tr>	
	<tr>
		<td>
			<input type="button" value="Update" onclick="submitForUpdate();"/>
		</td>
	</tr>		
	<tr>
		<td class="labelClass">
			&nbsp;
		</td>
	</tr>	
	<tr>
		<td class="labelClass">
			&nbsp;
		</td>
	</tr>	
	<tr>
		<td class="labelClass">
			 Adding New Announcement:
		</td>
	</tr>	
	<tr>
		<td>
			<textarea rows="3" cols="20" name="addAnnouncementArea">
			</textarea> 
		</td>
	</tr>
	<tr>
		<td>
			<input type="button" value="Add" onclick="submitForAdd();"/>
		</td>
	</tr>	
	<tr>
		<td>
			<input type="hidden" name="deleteID" />
			<input type="hidden" name="operation" />
		</td>
	</tr>
</table>
</form>

</body>
</html>